﻿<%@ Page Title="" Language="C#" MasterPageFile="~/master/admin.master" AutoEventWireup="true" Inherits="Admin_manage_forum_categories" Codebehind="manage-forum-categories.aspx.cs" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script src="<%= Utilities.SiteSettings.BaseUrl %>scripts/jquery.tmpl.js" type="text/javascript"></script>
	<link rel="stylesheet" href="<%= Utilities.SiteSettings.BaseUrl %>scripts/Grid/grid.css" />
    <script src="<%= Utilities.SiteSettings.BaseUrl %>scripts/Grid/pager.js"></script>
    <link rel="stylesheet" href="<%= Utilities.SiteSettings.BaseUrl %>scripts/Grid/loader.css" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
   <script type="text/javascript">
       var isEdit = false;
       var catId = -1;
       function CreateCategory(catName) {
           $.ajax({
               type: "POST",
               url: "manage-forum-categories.aspx/CreateCategory",
               data: "{catName:'" + catName + "'}",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               success: function (data) {
                  
               },
               error: function (msg) {
                   alert(msg);
               }
           });
       }

       function UpdateCategory(catName) {
          $.ajax({
               type: "POST",
               url: "manage-forum-categories.aspx/UpdateCategory",
               data: "{catName:'" + catName + "',catId:" + catId + "}",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               success: function (data) {

               },
               error: function (msg) {
                   alert(msg);
               }
           });
       }

       function deletecategory(catId) {

       var response=confirm('are you sure you want to delete?');
       if (response == true) {
           var serviceUrl = "../Services/SocialShare_Service.svc/SocialShare_Forum_Categories(" + catId+")";
           $.ajax({
               type: "DELETE",
               url: serviceUrl,
               data: "{}",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               success: function (data) {
                   LoadPager();
                   LoadData(currentPageIndex);
               },
               error: function (msg) {
                   alert(msg);
               }
           });
        }
       }
       function Filter() {
           currentPageIndex = 0;
           LoadPager();
           LoadData(0);
       }
       function LoadPager() {
           var serviceUrl = "../Services/SocialShare_Service.svc/SocialShare_Forum_Categories/$count";
           var filterVal = $("#txtTitle").val();
           if (filterVal != "") {
               serviceUrl = "../Services/SocialShare_Service.svc/SocialShare_Forum_Categories/$count/?$filter=substringof('" + filterVal + "',CategoryName)";
           }


           $.ajax({
               type: "GET",
               url: serviceUrl,
               data: "{}",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               success: function (data) {
                   totalCount = data;
                   SetPager(0);
               },
               error: function (msg) {
                   alert(msg);
               }
           });
       }

       function LoadData(currentIndex) {
           var serviceUrl = "../Services/SocialShare_Service.svc/SocialShare_Forum_Categories?" + "&$orderby=" + sortItem + " " + sortIDirection + "&$skip=" + (currentIndex * pageSize) + "&$top=" + pageSize;
           var filterVal = $("#txtTitle").val();
           if (filterVal != "") {
               serviceUrl = "../Services/SocialShare_Service.svc/SocialShare_Forum_Categories?$filter=substringof('" + filterVal + "',CategoryName)" + "&$orderby=" + sortItem + " " + sortIDirection + "&$skip=" + (currentIndex * pageSize) + "&$top=" + pageSize;
           }
           $.ajax({
               type: "GET",
               url: serviceUrl,
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               beforeSend: function () { $("#wait").show(); },
               complete: function () { $("#wait").hide(); },
               success: function (data) {
                   localDevelopers = data.d;
                   $("#users").html("");
                   $.each(data.d, function (i, item) {
                    
                       var col_prefix="cls-"+item.CategoryId;
                       var col1 = $("<td class='ui-state-custom-default'>").append(item.CategoryId).append("</td>");
                       var col2 = $("<td class='ui-state-custom-default " + col_prefix + "'>").append(item.CategoryName).append("</td>");

                       var createDt = null;

                       if (item.DateCreated != 'undefined' && item.DateCreated != null) {
                           var s = item.DateCreated.toString(); //	/Date(1296074816700)/
                           s = s.slice(6, 19);
                           var n = parseInt(s);
                           createDt = new Date(n);
                       }
                       var col3 = "";
                       if (createDt != null) {

                           col3 = $("<td class='ui-state-custom-default'>").append(dateFormat(createDt.toString(), "fullDate")).append("</td>");

                       }
                       else {
                           col3 = $("<td class='ui-state-custom-default'>").append(" ").append("</td>");
                       }


                       var col4 = $("<td class='ui-state-custom-default'>").append("<a href='#' onclick='javascript:deletecategory(" + item.CategoryId + ");'>delete</a>").append("</td>");
                       var col5 = $("<td class='ui-state-custom-default'>").append("<a href='#' onclick='javascript:OpenCategoryEditPopup(" + item.CategoryId + ");'>edit</a>").append("</td>");

                       $("<tr class='ui-state-custom-default'>").append(col1).append(col2).append(col3).append(col4).append(col5).append("</tr>").appendTo("#users"); ;

                   });
                   $('#dataTable tbody tr:even').addClass('grid-normal');
                   $('#dataTable tbody tr:odd').addClass('grid-alternate');
               },
               error: function (msg) {
                   alert(msg);
               }
           });


       }


       function OpenCategoryEditPopup(categoryId) {
           isEdit = true;
           catId = categoryId;
           var catName = $(".cls-" + categoryId).html();
           $("#catName").val(catName);
           $("#dialog-form").dialog("open");
           return false;
       }

       $(function () {
           $("#dialog-form").dialog({
               autoOpen: false,
               height: 300,
               width: 450,
               modal: true,
               buttons: {
                   "Save": function () {
                       var bValid = true;
                       var name = $("#catName").val();
                       if (isEdit) {
                           UpdateCategory(name);
                       }
                       else {
                           CreateCategory(name);
                       }
                       LoadPager();
                       LoadData(currentPageIndex);
                       $(this).dialog("close");
                   },
                   Cancel: function () {
                       $(this).dialog("close");
                   }
               },
               close: function () {

               }
           });
           $("#create-category")
			.button()
			.click(function () {
			    isEdit = false;
			    $("#catName").val("");
			    $("#dialog-form").dialog("open");
			    return false;
			});

       });
    </script>
    <section class="grid_12 clearfix omega">
<section class="grid_12 clearfix omega">
<section id="wait" class="progress grid_5">
  <section class="progress-container">
        <section class="progress-header">Loading, please wait...</section>
        <section class="progress-body">
            <img src="../Scripts/Grid/pre-loader.gif" />
        </section>
    </section>
  
  </section>
  <section class="grid_7">
  &nbsp;
  </section>
</section>
<section class="grid_12 omega">
<fieldset>
<legend class="title-regular">Filter</legend>
    <input id="txtTitle" type="text"  placeholder="CategoryName" /><input id="btnFilter" type="button" value="serach" class="button small red" onclick="javascript:Filter();return false;" />
    </fieldset>
<table id="dataTable" width="100%" class="ui-custom-grid">
<caption>Forum Categories</caption>
  <thead>
    <tr class="ui-state-custom-default">
      <th sort-data="CategoryId" class="grid-header-asc">ID#</th>
      <th sort-data="CategoryName" class="grid-header-asc">CategoryName</th>
      <th sort-data="DateCreated" class="grid-header-asc">DateCreated</th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody id="users">
  </tbody>
  <tfoot>
  <tr> <th align="center" colspan="5">
  
   <section class="grid_10 clearfix grid-pager">
<a href="" class="button small blue pager-first" data-page="first">First</a>
    <a href="" class="button small green pager-prev" data-page="prev">Prev</a>	
	<span>
		Page <span class="current"></span>/<span class="total">0</span>,
		Total records <span class="totalRecords">0</span>
	</span>
	<a href=""  class="button small green pager-next" data-page="next">Next</a>
	<a href=""  class="button small blue pager-last" data-page="last">Last</a>
</section>
  </th>
  
  </tr>
 
  </tfoot>
</table>
<button id="create-category">Create New Category</button>
</section>
</section>

<div id="dialog-form" title="Create New category">
<p class="validateTips">All form fields are required.</p>
<fieldset>
		<label for="name">Category Name</label>
		<input type="text" name="name" id="catName" class="text ui-widget-content ui-corner-all" />
</fieldset>
</div>
</asp:Content>

